package com.bolt.admin.web.view.freemarker;

import com.bolt.admin.module.sys.entity.TableConfig;
import com.bolt.admin.module.sys.entity.TableHeadConfig;
import com.bolt.admin.module.sys.service.TableConfigService;
import com.bolt.common.utils.SpringContextUtil;
import com.bolt.common.utils.StrUtil;
import com.bolt.core.view.components.ComponentsBase;
import com.bolt.core.view.components.DataGrid;
import com.bolt.core.view.components.TableHead;
import com.bolt.core.view.config.UIProperties;
import com.bolt.core.view.freemarker.TagModel;
import com.bolt.core.view.templates.FreemarkerTemplateEngine;
import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/4/17.
 */
public class DataGridModel extends TagModel {

    String getId(Map params) {
        return getStrParam(params, "id");
    }

    String getDomId(Map params) {
        return getStrParam(params, "domId");
    }

    @Override
    public ComponentsBase getUI(Map params) {
        DataGrid dataGrid =	createTableByContext(params);
        if(null == dataGrid){
            return createTableByDb(params);
        }else{
            return dataGrid;
        }
    }


    private DataGrid createTableByContext(Map params){
        Object dbTable =  getRequest().getAttribute(getId(params));
        if(null != dbTable){
            return (DataGrid)dbTable;
        }else{
            return null;
        }
    }

    private DataGrid createTableByDb(Map params){
        String id =   getId(params);
        String domId = getDomId(params);
        TableConfigService tableconfigService = SpringContextUtil.getBean(TableConfigService.class);
        FreemarkerTemplateEngine engine = SpringContextUtil.getBean(FreemarkerTemplateEngine.class);
        UIProperties uiProperties = SpringContextUtil.getBean(UIProperties.class);
        TableConfig tableconfig = tableconfigService.findTableConfig("",id);

        DataGrid table = new DataGrid();
        table.setTheme(uiProperties.getTheme());
        table.setTemplateDir(uiProperties.getDefaultPath());
        table.setTemplateEngine(engine);
        table.setId(StrUtil.isNotBlank(domId)?domId:id);
        table.setTableId(StrUtil.isNotBlank(domId)?domId:id);
        table.setTableTitle(tableconfig.getTableTitle());
        String s = getRequest().getQueryString();
        if (StrUtil.isNotBlank(s)&& StrUtil.isNotBlank(tableconfig.getTableUrl())) {
            table.setUrl(tableconfig.getTableUrl()+"?"+s);
        }else{
            table.setUrl(tableconfig.getTableUrl());
        }
        table.setToolbar(tableconfig.getToolbar());
        table.setPagination(tableconfig.isPagination());
        table.setAutoCreate(tableconfig.isAutoCreate());
        table.setCheckBox(tableconfig.isCheckbox());
        table.setShowLinenumber(false);
        List<TableHead> heads = new ArrayList<>();
        List<TableHeadConfig>  tableHeads = tableconfig.getTableHeads();
        Collections.sort(tableHeads);
        for(TableHeadConfig tableHead :tableHeads){
            TableHead head = new TableHead();
            head.setField(tableHead.getField());
            head.setTitle(tableHead.getTitle());
            head.setListAlign(tableHead.getAlign());
            head.setFormat(StrUtil.isEmpty(tableHead.getRender())?null:tableHead.getRender());
            head.setHidden(tableHead.getHidden());
            head.setWidth(tableHead.getWidth());
            head.setEditor(tableHead.getEditType());
            head.setOrder(tableHead.getSortable());
            heads.add(head);

        }
        table.setHeads(heads);
        SpringContextUtil.inject(table);
        return table;


    }


    @Override
    public void render(ComponentsBase component, Environment env, Map params, TemplateDirectiveBody body) throws IOException, TemplateException {
        component.start(getRequest(),getResponse(),env.getOut());
        component.end(getRequest(),getResponse(),env.getOut());
    }
}

 